package com.ccl.iot.dvr.utils;

/**
 * Created by HD on 2016/11/30.
 */

import android.net.TrafficStats;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;


/**
 * 显示即时流量工具类
 */
public class NetWorkSpeedUtils {

    private long lastTotalRxBytes = 0;
    private long lastTimeStamp = 0;

    private static NetWorkSpeedUtils mInstance = null;
    private HandlerThread mHandlerThread;
    private Handler mBackHandler;

    private boolean isUpdateInfo;

    private static final int MSG_UPDATE_INFO = 0x110;

    private NetWorkSpeedUtils() {

    }

    public static NetWorkSpeedUtils getInstance() {
        if (mInstance == null) {
            synchronized (NetWorkSpeedUtils.class) {
                if (mInstance == null) {
                    mInstance = new NetWorkSpeedUtils();
                }
            }
        }
        return mInstance;
    }


    /**
     * 开始定时器任务
     */
    public void startShowNetSpeed() {
        LogUtil.i("DVR", "startShowNetSpeed");
        lastTotalRxBytes = getTotalRxBytes();
        lastTimeStamp = System.currentTimeMillis();
        isUpdateInfo = true;
        initBackThread();

    }

    private void initBackThread() {
        LogUtil.i("DVR", "initBackThread");

        if (mHandlerThread == null || (mHandlerThread != null && !mHandlerThread.getName().equalsIgnoreCase("backThread"))) {
            LogUtil.i("DVR", "新建mHandlerThread");
            mHandlerThread = new HandlerThread("backThread");
            mHandlerThread.start();
        }

        if (mBackHandler == null) {
            mBackHandler = new Handler(mHandlerThread.getLooper()) {
                @Override
                public void handleMessage(Message msg) {
                    showNetSpeed();
                    if (isUpdateInfo) {
                        mBackHandler.sendEmptyMessageDelayed(MSG_UPDATE_INFO, 2000);
                    }
                }
            };
        }

        //发送消息
        mBackHandler.sendEmptyMessageDelayed(MSG_UPDATE_INFO, 2000);


    }

    /**
     * 获取总流量
     *
     * @return
     */
    private long getTotalRxBytes() {
        return TrafficStats.getTotalRxBytes() / 1024;//转为KB
    }

    private void showNetSpeed() {
//        LogUtil.i("DVR","showNetSpeed");
        long nowTotalRxBytes = getTotalRxBytes();
        long nowTimeStamp = System.currentTimeMillis();
        long speed = ((nowTotalRxBytes - lastTotalRxBytes) * 1000 / (nowTimeStamp - lastTimeStamp));//毫秒转换

        lastTimeStamp = nowTimeStamp;
        lastTotalRxBytes = nowTotalRxBytes;

        DVRUtil.mNetWorkSpeed = speed;

//        Log.i("DVR","speed:" + speed );

    }

    public void pauseTask() {
        isUpdateInfo = false;
    }

    public void cannelTask() {

        isUpdateInfo = false;

        if (mHandlerThread != null) {
            mHandlerThread.quit();
            mHandlerThread = null;
        }

        if (mBackHandler != null) {
            mBackHandler = null;
        }
    }

    public Handler getBackHandler() {
        return mBackHandler;
    }

    public void sendMessage() {
        isUpdateInfo = true;
        //先移除旧的
        mBackHandler.removeMessages(MSG_UPDATE_INFO);
        //发送消息
        mBackHandler.sendEmptyMessageDelayed(MSG_UPDATE_INFO, 2000);
    }
}